package com.jm.offer.easy;

import com.jm.offer.ListNode;
import lombok.Data;

import java.util.Stack;

/**
 * @Description https://leetcode-cn.com/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof/
 * @date 2022/4/4 7:45
 */
public class _06从尾到头打印链表 {
    public static void main(String[] args) {
        ListNode listNode = new ListNode(3);
        ListNode listNode1 = new ListNode(2);
        listNode.setNext(listNode1);
        listNode1.setNext(new ListNode(1));
        int[] ints = reversePrint(listNode);
        if (ints != null && ints.length != 0 ){
            for (int anInt : ints) {
                System.out.println(anInt);
            }
        }
    }

    public static int[] reversePrint(ListNode head) {
        if (head == null){
            return new int[0];
        }
        Stack<ListNode> stack = new Stack<>();
        ListNode temp = head;
        while (temp != null){
            stack.push(temp);
            temp = temp.getNext();
        }
        int size = stack.size();
        int[] arr = new int[size];
        for (int i = 0; i < size; i++) {
            arr[i] = stack.pop().getVal();
        }
        return arr;
    }
}


